<script>
//需要导入什么：reactive 把数据变成响应式数据    toRefs：把数据展开，达到写代码更简洁的目的
import { reactive, toRefs } from "vue";
// 还要导入什么：axios  达到前后端交互的目的
import axios from "axios";
//导入router  路由器
import { useRouter } from "vue-router";

import { setSessionStorage } from "@/common";

import dentity from "./Dentity.vue"

export default {  //前端js代码写法使用组合式API
    setup() {
        //实例化router对象
        const router = useRouter();

        //定义需要绑定的数据
        const state = reactive({
            users: {
                userId: "",
                password: ""
            }
        });

        //当用户点击登录按钮时调用login函数
        function login() {
            //算法怎么写呢？1.非空校验  2.递交请求给服务端（结果有两种情况，成功或者失败）
            // console.log(state.users.userId)
            // console.log(state.users.password)

            if (state.users.userId == '') {
                alert('手机号码不能为空');
                return;
            }

            if (state.users.password == '') {
                alert('密码不能为空');
                return;
            }

            //都不为空，那么代码继续往下走
            axios.post('/api/users/login', state.users).then((response) => {
                console.log(response.data);
                let u = response.data;//  u----ApiRestResponse
                let user = u.data;
                if (u.status == 200) {
                    //先保存用户信息到会话域sessionStorage
                    setSessionStorage("user", user);
                    router.push('/index');
                } else {
                    alert(u.desc);
                    return;
                }

            }).catch((error) => {
                console.log(error);
            });
        }

        function forgotPassword() {
            router.push('/forgotPassword');
        }

        return {   //把数据和函数暴露出去
            ...toRefs(state),
            login,
            forgotPassword
        }
    },

    data() {
        return {
            // 表单提交内容
            formLogin: {
                code: "", //验证码输入框
            },
            identifyCodes: "1234567890abcdefjhijklinopqrsduvwxyz", //随机串内容,从这里随机抽几个显示验证码
            identifyCode: "", //验证码图片内容
        };
    },
    mounted() {
        // 初始化验证码
        this.identifyCode = "";
        //参数：（1）随机串内容。（2）验证码显示位数
        this.makeCode(this.identifyCodes, 4);
    },
    methods: {
        // 重置验证码
        refreshCode() {
            this.identifyCode = "";
            this.makeCode(this.identifyCodes, 4);
        },
        //获取验证码的值
        makeCode(o, l) {
            for (let i = 0; i < l; i++) {
                //通过循环获取字符串内随机几位
                this.identifyCode +=
                    this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
            }
        },
        //随机数字：用于当角标拿字符串的值
        randomNum(min, max) {
            return Math.floor(Math.random() * (max - min) + min);
        },
        submit() {

            console.log("验证码:", this.identifyCode);
            console.log("用户输入的验证码:", this.formLogin.code);
            if (this.identifyCode !== this.formLogin.code) {
                alert('验证码输入错误');
                console.log("不行");
                this.refreshCode(); // 刷新验证码
                return;
            }
            this.login();
        },
    },
    components: {
        dentity,
    },
}
</script>


<template>
    <!-- 总容器 -->
    <div class="wrapper">
        <h1>体检预约-登录</h1>
        <section>
            <div class="input-box">
                <i class="fa fa-user-o"></i>
                <input type="text" placeholder="输入手机号" v-model="users.userId">
            </div>
            <div class="input-box">
                <i class="fa fa-lock"></i>
                <input type="password" placeholder="输入登录密码" v-model="users.password">
            </div>
            <div class="dentity">
                
                    <input placeholder="请输入验证码" v-model="formLogin.code" style="width: 70%;">

                        <div class="login-code" width="50%" @click="refreshCode">
                            <dentity :identifyCode="identifyCode"></dentity>
                        </div>
            </div>   
            <div class="reg-box">
            <p onclick="location.href='register'">注册</p>
            <p onclick="location.href='forgotPassword'">忘记密码？</p>
        </div>
            <div class="button-box" @click="submit">登录</div>
        </section>

        <footer>
            <div>
                <div class="line"></div>
                <p>有疑问请联系客服</p>
                <div class="line"></div>
            </div>
            <p>4008-XXX-XXX</p>
        </footer>
    </div>

</template>


<style scoped>
/* scoped  :局部样式 */
/*********************** 总容器 ***********************/
.wrapper {
    width: 100%;
    height: 100%;
    background-image: linear-gradient(45deg, #266C9F, #266C9F, #7EB059);
    overflow: hidden;
    padding-bottom: 220px;
}

/*********************** 标题部分 ***********************/
h1 {
    text-align: center;
    color: #FFF;
    font-size: 9.5vw;
    font-weight: 500;
    margin-top: 13vh;
    margin-bottom: 3vh;
}

/*********************** 内容部分 ***********************/
section {
    width: 86vw;
    margin: 0 auto;
    background-color: #FFF;
    border-radius: 2.4vw;

    box-sizing: border-box;
    padding: 6vw;
}

section .input-box {
    width: 100%;
    height: 12vw;
    border: solid 1px #CCC;
    border-radius: 2vw;
    margin-top: 5vw;

    display: flex;
    align-items: center;
}

section .input-box i {
    margin: 0 3.6vw;
    font-size: 5.4vw;
    color: #CCC;
}

section .input-box input {
    border: none;
    outline: none;
}

.dentity{
    width: 100%;
    height: 12vw;
    border-radius: 2vw;
    margin-top: 5vw;
    display: flex;
    align-items: center;
}
.dentity input{
    border: none;
    outline: none;
}


section .reg-box {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 5vw;
    margin-bottom: 2vw;
}

section .reg-box p {
    font-size: 3.3vw;
    color: #2D727E;
    user-select: none;
    cursor: pointer;
}

section .button-box {
    width: 100%;
    height: 13vw;
    border-radius: 2vw;
    background-color: #70B0BC;

    text-align: center;
    line-height: 13vw;
    font-size: 4.6vw;
    color: #FFF;
    letter-spacing: 1vw;

    user-select: none;
    cursor: pointer;
}

/*********************** footer部分 ***********************/
footer {
    width: 86vw;
    margin: 0 auto;
    margin-top: 12vw;
    font-size: 3.8vw;
    color: #FFF;
}

footer div {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

footer div .line {
    width: 22vw;
    height: 1px;
    background-color: #FFF;
}

footer>p {
    text-align: center;
    margin-top: 2vw;
}
</style>